<template>
  <div class="container">
    <div class="header">
      <el-input
        v-model="input"
        placeholder="请输入菜品名称"
        clearable
        @keyup.enter.native="handleSearch"
        @input="debouncedHandleInput"
      >
        <i slot="prefix" class="el-input__icon el-icon-search"></i>
      </el-input>
      <div class="nav">
        <el-link
          type="primary"
          :underline="false"
          @click="handleDelete(multipleSelection)"
          >批量删除</el-link
        >
        <el-divider direction="vertical"></el-divider>
        <el-link
          type="primary"
          :underline="false"
          @click="handleChange('1', multipleSelection)"
          >批量启售</el-link
        >
        <el-divider direction="vertical"></el-divider>
        <el-link
          type="primary"
          :underline="false"
          @click="handleChange('0', multipleSelection)"
          >批量停售</el-link
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin-left: 40px"
          @click="$router.push({ name: 'AddDish', query: { isEdit: 'false' } })"
          >新建菜品</el-button
        >
      </div>
    </div>

    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      ref="multipleTable"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"> </el-table-column>
      <el-table-column prop="name" label="菜品名称"></el-table-column>
      <el-table-column prop="image" label="图片">
        <template v-slot="{ row }">
          <el-image
            style="width: 40px; height: 40px"
            :src="`${env}/common/download?name=${row.image}`"
            :preview-src-list="[`${env}/common/download?name=${row.image}`]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="菜品分类"></el-table-column>
      <el-table-column prop="price" label="售价">
        <template v-slot="{ row }">
          {{ `￥${row.price / 100}` }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="售卖状态">
        <template v-slot="{ row }">
          <div>
            {{ row.status === 1 ? '启售' : '停售' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="最后操作时间"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link
            type="primary"
            @click="
              $router.push({
                name: 'EditDish',
                query: { isEdit: 'true', id: row.id }
              })
            "
            >修改</el-link
          >
          <el-divider direction="vertical" />
          <!-- <el-link type="primary" :underline="false">起售</el-link> -->
          <el-popconfirm
            title="确认调整该账号的状态?"
            @onConfirm="handleChange(row.status === 1 ? 0 : 1, [row])"
          >
            <el-link type="danger" slot="reference" :underline="false">
              {{ row.status ? '停售' : '启售' }}</el-link
            >
          </el-popconfirm>
          <el-divider direction="vertical" />
          <el-popconfirm
            title="确认删除该菜品？"
            @onConfirm="handleDelete([row])"
          >
            <el-link type="primary" slot="reference" :underline="false"
              >删除</el-link
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="footer">
      <el-pagination
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import _ from 'lodash'
import { getDishes, deleteDishes, changeDishes } from '@/api/dish'
export default {
  data () {
    return {
      env: process.env.VUE_APP_BASE_API,
      input: '',
      tableData: [],
      pageInfo: { page: 1, pageSize: 10 },
      total: 0,
      multipleSelection: []
    }
  },
  created () {
    this.init()
    // 创建防抖函数
    this.debouncedHandleInput = _.debounce(this.handleInput, 500)
  },
  methods: {
    handleDelete (array) {
      if (array.length) {
        deleteDishes({
          ids: array.map(item => item.id).join()
        }).then(res => {
          this.$message.success('删除成功')
          this.init()
        })
      } else {
        this.$message.warning('请勾选')
      }
    },
    async handleChange (status, array) {
      await changeDishes(status, {
        ids: array.map(item => item.id).join()
      }).then(res => {
        this.$message.success('改变成功')
        this.init()
      })
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    init () {
      getDishes(this.pageInfo).then(res => {
        const { records, total } = res.data
        this.tableData = records
        this.total = total
      })
    },
    handleSizeChange (val) {
      this.pageInfo.pageSize = val
      this.pageInfo.page = 1
      this.init()
    },
    handleCurrentChange (val) {
      this.pageInfo.page = val
      this.init()
    },
    handleSearch () {
      getDishes({ ...this.pageInfo, name: this.input }).then(res => {
        const { records, total } = res.data
        this.tableData = records
        this.total = total
      })
    },
    handleInput () {
      this.handleSearch()
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 35px;
  .header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    .el-input {
      width: 250px;
      i {
        cursor: pointer;
      }
    }
  }
  .footer {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }
}
</style>
